/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.telas;

import br.edu.banco.Banco;
import br.edu.banco.BancoDAO;
import br.edu.banco.BancoTableModel;
import br.edu.cheque.Cheque;
import br.edu.cheque.ChequeDAO;
import br.edu.cheque.ChequeTableModel;
import br.edu.cliente.Cliente;
import br.edu.cliente.ClienteDAO;
import br.edu.cliente.ClienteTableModel;
import br.edu.situacao.Situacao;
import br.edu.situacao.SituacaoDAO;
import br.edu.situacao.SituacaoTableModel;
import br.edu.util.Util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author 'Pedro
 */
public class TCheque extends javax.swing.JDialog {
    
    private Cheque cheque = new Cheque();
    private ChequeDAO dao = new ChequeDAO();

    /**
     * Creates new form Curso
     */
    public TCheque() {
        initComponents();
        setModal(true);
        setLocationRelativeTo(null);
        setTitle("Cheque");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btRemover = new javax.swing.JButton();
        btPesquisar = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        edtNRCheque = new javax.swing.JTextField();
        edtCorrentista = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        edtBanco = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        edtCidade = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        edtCliente = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        edtValor = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        edtData = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        edtBomPara = new javax.swing.JFormattedTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/imagens/novo_1.png"))); // NOI18N
        btNovo.setToolTipText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        jPanel1.add(btNovo, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 240, 42, 33));

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/imagens/save_1.png"))); // NOI18N
        btSalvar.setToolTipText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jPanel1.add(btSalvar, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 240, 40, 33));

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/imagens/remove_1.png"))); // NOI18N
        btRemover.setToolTipText("Excluir");
        btRemover.setEnabled(false);
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });
        jPanel1.add(btRemover, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 240, 37, 33));

        btPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/imagens/procurar_1.png"))); // NOI18N
        btPesquisar.setToolTipText("Pesquisar");
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });
        jPanel1.add(btPesquisar, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 240, 37, 33));

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/imagens/SA.png"))); // NOI18N
        btSair.setToolTipText("Sair");
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });
        jPanel1.add(btSair, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 240, -1, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Nr. Cheque.: *");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 41, -1, -1));

        edtNRCheque.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel1.add(edtNRCheque, new org.netbeans.lib.awtextra.AbsoluteConstraints(97, 38, 119, -1));

        edtCorrentista.setEditable(false);
        edtCorrentista.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel1.add(edtCorrentista, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 70, 280, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Correntista.: * ");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

        edtBanco.setEditable(false);
        edtBanco.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel1.add(edtBanco, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 100, 160, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Banco.: * ");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, -1, -1));

        edtCidade.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel1.add(edtCidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 130, 160, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Cidade.: * ");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, -1, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setText("Cliente.: * ");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1));

        edtCliente.setEditable(false);
        edtCliente.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel1.add(edtCliente, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 160, 160, -1));

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel9.setText("Valor.: * ");
        jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, -1, -1));

        edtValor.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel1.add(edtValor, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 190, 120, -1));

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 160, 30, -1));

        jButton2.setText("jButton1");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 70, 30, -1));

        jButton3.setText("jButton1");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 100, 30, -1));

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel10.setText("Data.: * ");
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 40, -1, -1));

        try {
            edtData.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        edtData.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel1.add(edtData, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 40, 160, -1));

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel11.setText("Bom P/.: * ");
        jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 190, -1, -1));

        try {
            edtBomPara.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        edtBomPara.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel1.add(edtBomPara, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 190, 160, -1));

        jButton5.setText("N");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 70, 40, -1));

        jButton6.setText("N");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 100, 40, -1));

        jButton7.setText("N");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 160, 40, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 476, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        limpaCampos();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if (Util.chkVazio(edtNRCheque.getText(),edtBanco.getText(),edtBomPara.getText(),
                edtCidade.getText(),edtCliente.getText(),edtCorrentista.getText(),
                edtData.getText(),edtValor.getText())) {
            if (cheque == null) {
                cheque = new Cheque();
            }
            cheque.setNrcheque(edtNRCheque.getText());
            cheque.setCidade(edtCidade.getText());
            cheque.setValor(Double.parseDouble(edtValor.getText()));
            
            String dataString = edtData.getText();
            try {
                DateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
                java.util.Date data;
                data = new java.util.Date(fmt.parse(dataString).getTime());
                cheque.setData(data);
            } catch (ParseException ex) {
                System.out.println(ex.getMessage());
            }
            
            dataString = edtBomPara.getText();
            try {
                DateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
                java.util.Date data;
                data = new java.util.Date(fmt.parse(dataString).getTime());
                cheque.setBompara(data);
            } catch (ParseException ex) {
                System.out.println(ex.getMessage());
            }
            
            int id = cheque.getId();
            dao = new ChequeDAO();
            if (dao.salvar(cheque)) {
                JOptionPane.showMessageDialog(rootPane, "Cheque: " + cheque.getNrcheque()
                        + ", " + ((id == 0) ? "cadastrada" : "alterada") + " com sucesso!");
                limpaCampos();
            }
            cheque = new Cheque();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
        if (cheque != null) {
            if (cheque.getId() != 0) {
                if (JOptionPane.showConfirmDialog(rootPane, "Deseja excluir o cheque " + cheque.getNrcheque()
                        + "?", "", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION) {
                    if (dao.remover(cheque)) {
                        JOptionPane.showMessageDialog(rootPane, "Cheque " + cheque.getNrcheque()
                                + ", excluída com sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Não foi possível excluir o cheque "
                                + cheque.getNrcheque(),
                                "Erro ao Excluir", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
        limpaCampos();
    }//GEN-LAST:event_btRemoverActionPerformed

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        List<Cheque> lista = ((!edtNRCheque.getText().equals("")) ? dao.pesquisarNrChequeLike(edtNRCheque.getText()) : dao.listar());
        ChequeTableModel stm = new ChequeTableModel(lista);
        Object o = TPesquisa.exibeTela(stm, "Cheque");
        if (o != null) {
            cheque = dao.pesquisarCodigo(Integer.valueOf(String.valueOf(o)));
            edtBanco.setText(cheque.getBanco().getDescricao());
            edtCidade.setText(cheque.getCidade());
            edtCliente.setText(cheque.getCliente().getNome());
            edtCorrentista.setText(cheque.getCorrentista().getNome());
            edtNRCheque.setText(cheque.getNrcheque());
            edtValor.setText(String.valueOf(cheque.getValor()));
            btRemover.setEnabled(true);
            Date dt = cheque.getData();
            SimpleDateFormat dfdt;
            dfdt = new SimpleDateFormat("dd/MM/yyyy");
            edtData.setText(dfdt.format(dt));
           
            dt = cheque.getBompara();
            edtBomPara.setText(dfdt.format(dt));       
        }
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        dispose();
    }//GEN-LAST:event_btSairActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        ClienteDAO dao = new ClienteDAO();
        List<Cliente> lista = dao.listar();
        ClienteTableModel stm = new ClienteTableModel(lista);
        Object o = TPesquisa.exibeTela(stm, "Correntista");
        Cliente c;
        if (o != null) {
            c = dao.pesquisarCodigo(Integer.valueOf(String.valueOf(o)));
            cheque.setCorrentista(c);
            edtCorrentista.setText(c.getNome());
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        ClienteDAO dao = new ClienteDAO();
        List<Cliente> lista = dao.listar();
        ClienteTableModel stm = new ClienteTableModel(lista);
        Object o = TPesquisa.exibeTela(stm, "Cliente");
        Cliente c;
        if (o != null) {
            c = dao.pesquisarCodigo(Integer.valueOf(String.valueOf(o)));
            cheque.setCliente(c);
            edtCliente.setText(c.getNome());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        BancoDAO dao = new BancoDAO();
        List<Banco> lista = dao.listar();
        BancoTableModel stm = new BancoTableModel(lista);
        Object o = TPesquisa.exibeTela(stm, "Banco");
        Banco b;
        if (o != null) {
            b = dao.pesquisarCodigo(Integer.valueOf(String.valueOf(o)));
            cheque.setBanco(b);
            edtBanco.setText(b.getDescricao());
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        TCliente tc = new TCliente();
        tc.setVisible(true);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        TCliente tc = new TCliente();
        tc.setVisible(true);
    }//GEN-LAST:event_jButton7ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TCheque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TCheque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TCheque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TCheque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TCheque().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton btSair;
    private javax.swing.JButton btSalvar;
    private javax.swing.JTextField edtBanco;
    private javax.swing.JFormattedTextField edtBomPara;
    private javax.swing.JTextField edtCidade;
    private javax.swing.JTextField edtCliente;
    private javax.swing.JTextField edtCorrentista;
    private javax.swing.JFormattedTextField edtData;
    private javax.swing.JTextField edtNRCheque;
    private javax.swing.JFormattedTextField edtValor;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    private void limpaCampos() {
        edtNRCheque.setText("");
        btRemover.setEnabled(false);
        edtBanco.setText("");
        edtCidade.setText("");
        edtCliente.setText("");
        edtCorrentista.setText("");
        edtNRCheque.setText("");
        edtValor.setText("");
        cheque = new Cheque();
        edtBomPara.setText("");
        edtData.setText("");
    }
}
